#include "common.h"
#include "font.h"
#include "i2c2.h"
#include "sht20.h"
#include "oled_ssd1306.h"
#include "spi0.h"
#include "string.h"

#include "uart1.h"
#include "rtc_ds1339.h"
#include "PCA9557.h"

extern "C" { 
#include "w25q256.h"
}
#include<regex>

#define ESP32(args) args"\r\n"
char http_url[]="AT+HTTPCLIENT=2,0,\"https://api.seniverse.com/v3/weather/now.json?key=SZRaEMZNu4ieYZ3R3&location=hangzhou&language=zh-Hans&unit=c\",,,2\r\n";

void esp32_example();

void esp32_http();

void esp32_ble();

uart ctrl_uart;//全局变量

static void my_handler(int sig){
    //防止esp32处于透传模式
    ctrl_uart.uart_send("+++",3);
    exit(0);
}

int main(int argc, char* argv[])
{
    // uart
    //esp32_example();
    //esp32_http();
    esp32_ble();
    return 0;
}


void esp32_http()
{
    char get_buffer[2048] = {0x00};
    int ret=0;
    char* chret=NULL;

    if(ctrl_uart.uart_open() < 0)
    {
        ERROR_LOG("uart_open error");
        exit(1);
    }

    if((ret=ctrl_uart.uart_set_option(115200,8,'N',1)) < 0)
    {
        ERROR_LOG("uart_set_option error");
        exit(1);
    }

    // cout << "恢复出厂设置" << endl;
    // ctrl_uart.uart_send(ESP32("AT+RESTORE"), strlen(ESP32("AT+RESTORE")));
    // usleep(1000000);
    // ret = ctrl_uart.uart_read(get_buffer,2048);
    // cout << get_buffer << endl;
    // memset(get_buffer,0x00,ret);

    // while(1)
    // {
    //     usleep(1000000);
    //     ctrl_uart.uart_read(get_buffer,2048);
    //     chret = strstr(get_buffer,"OK");
    //     if(chret)
    //     {
    //         cout << get_buffer << endl;
    //         memset(get_buffer,0x00,ret);
    //         break;
    //     }       
    // }


    cout <<"设置wifi 模式为station"<< endl;
    ctrl_uart.uart_send(ESP32("AT+CWMODE=1"), strlen(ESP32("AT+CWMODE=1")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);


    cout << "连接路由器" << endl;
    ctrl_uart.uart_send(ESP32("AT+CWJAP=\"abc\",\"17816376987\""), strlen(ESP32("AT+CWJAP=\"abc\",\"17816376987\"")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);
    usleep(5000000);


    cout << "发送http 请求 " << endl;
    ctrl_uart.uart_send(http_url,strlen(http_url));
    usleep(1000000);
    ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);
    while (1)
    {
        ret = ctrl_uart.uart_read(get_buffer,2048);
        if(ret != 0)
        {
            cout << get_buffer << endl;
            break;
        }
        memset(get_buffer,0x00,ret);
        usleep(1000000);
    }
}

void esp32_example()
{
    int ret;
    // uart ctrl_uart;
    if(ctrl_uart.uart_open() < 0)
    {
        ERROR_LOG("uart_open error");
        exit(1);
    }

    if((ret=ctrl_uart.uart_set_option(115200,8,'N',1)) < 0)
    {
        ERROR_LOG("uart_set_option error");
        exit(1);
    }

    char get_buffer[1000] = {0x00};

    cout << "取消esp32回显" << endl;
    ctrl_uart.uart_send(ESP32("ATE0"), strlen(ESP32("ATE0")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout << "查询AT是否正常工作" << endl;
    ctrl_uart.uart_send(ESP32("AT"), strlen(ESP32("AT")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout << "查询AT版本信息" << endl;
    ctrl_uart.uart_send(ESP32("AT+GMR"), strlen(ESP32("AT+GMR")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout << "设置esp32为SoftAP" << endl;
    ctrl_uart.uart_send(ESP32("AT+CWMODE=2"), strlen(ESP32("AT+CWMODE=2")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout << "设置ESP32热点账号和密码" << endl;
    // ctrl_uart.uart_send("AT+CWJAP=",9);
    // string apName;
    // cout << "输入要设置的热点名称" << endl;
    // cin >> apName;
    // ctrl_uart.uart_send("\"",1);
    // ctrl_uart.uart_send(apName.c_str(),apName.length());
    // ctrl_uart.uart_send("\",\"",3);
    // string apPass;
    // cout << "输入要设置的密码" << endl;
    // cin >> apPass;
    // ctrl_uart.uart_send(apPass.c_str(),apPass.length);
    // ctrl_uart.uart_send("\",5,3\r\n",7);
    //可能要多等会才有返回值
    ctrl_uart.uart_send(ESP32("AT+CWSAP=\"ESP32_SOFTAP\",\"1234567890\",5,3"), strlen(ESP32("AT+CWSAP=\"ESP32_SOFTAP\",\"1234567890\",5,3")));
    usleep(1000000);
    while (1)
    {
        ret = ctrl_uart.uart_read(get_buffer,1000);
        if(ret != 0)
        {
            cout << get_buffer << endl;
            break;
        }
        usleep(1000000);
    }
    memset(get_buffer,0x00,ret);

    cout << "wait PC connect............" << endl;
    string str;
    regex rx("STA_CONNECTED");
    smatch m;
    while (1)
    {
        ret = ctrl_uart.uart_read(get_buffer,1000);
        if(ret != 0)
        {
            str = get_buffer;
            bool result = regex_search(str,m,rx);
            if(result == 1)
            {
                cout << m.str() << endl;
                cout << get_buffer << endl;
                break;
            }
        }
        usleep(1000000);
    }
    ctrl_uart.uart_clearbuffer();
    memset(get_buffer,0x00,ret);
    
    cout << "确认PC是否开启UDP服务器" << endl;
    while (1)
    {
        cout << "如果已经开启，请输入yes！" << endl;
        string str;
        cin >> str;
        if(str == "yes")
        {
            break;
        }
        usleep(1000000);
    }

    cout << "连接PC的UDP服务器" << endl;
    ctrl_uart.uart_send(ESP32("AT+CIPSTART=\"UDP\",\"192.168.4.2\",8080,2233,0"), strlen(ESP32("AT+CIPSTART=\"UDP\",\"192.168.4.2\",8080,2233,0")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout << "开启 UART Wi-Fi 传输模式" << endl;
    ctrl_uart.uart_send(ESP32("AT+CIPMODE=1"), strlen(ESP32("AT+CIPMODE=1")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);



    signal(SIGINT, my_handler); 


    cout << "开启透传" << endl;
    ctrl_uart.uart_send(ESP32("AT+CIPSEND"), strlen(ESP32("AT+CIPSEND")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,1000);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    while (1)
    {
        char touchuan[1000] = {0x00};
        cout << "输入任意字符开启传输：" << endl;
        cin >> touchuan;
        for(int i=0;i<1000;i++)
        {
            if(touchuan[i] == 0x00)
            {
                break;
            }
            ctrl_uart.uart_send(touchuan+i,1);
        }
    }
}


void esp32_ble()
{
    char get_buffer[2048] = {0x00};
    int ret=0;
    char* chret=NULL;

    if(ctrl_uart.uart_open() < 0)
    {
        ERROR_LOG("uart_open error");
        exit(1);
    }

    if((ret=ctrl_uart.uart_set_option(115200,8,'N',1)) < 0)
    {
        ERROR_LOG("uart_set_option error");
        exit(1);
    }


    cout << "[INFO] 1.恢复出厂设置" << endl;
    ctrl_uart.uart_send(ESP32("AT+RESTORE"), strlen(ESP32("AT+RESTORE")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);
    usleep(5000000);

    cout <<"[INFO] 2.初始化 Bluetooth LE 功能"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEINIT=2"), strlen(ESP32("AT+BLEINIT=2")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout <<"[INFO] 3.ESP32 Bluetooth LE 服务端创建服务"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEGATTSSRVCRE"), strlen(ESP32("AT+BLEGATTSSRVCRE")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout <<"[INFO] 4.ESP32 Bluetooth LE 服务端开启服务"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEGATTSSRVSTART"), strlen(ESP32("AT+BLEGATTSSRVSTART")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout <<"[INFO] 5.ESP32 蓝牙 LE 服务器获取其 MAC 地址"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEADDR?"), strlen(ESP32("AT+BLEADDR?")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout <<"[INFO] 6.设置 Bluetooth LE 广播数据"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEADVDATA=\"0201060A09457370726573736966030302A0\""), strlen(ESP32("AT+BLEADVDATA=\"0201060A09457370726573736966030302A0\"")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    
    cout <<"[INFO] 7.ESP32 Bluetooth LE 服务端开始广播"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEADVSTART"), strlen(ESP32("AT+BLEADVSTART")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    //wait connect
    while(1)
    {
        usleep(1000000);
        ret = ctrl_uart.uart_read(get_buffer,2048);
        if(ret !=0 )
        {
            if(strstr(get_buffer,"+BLECONN"))
            {
                cout <<"[INFO] 8."<<get_buffer << endl;
                break;
            }
        }
    }

    cout <<"[INFO] 9.ESP32 Bluetooth LE 服务端查询服务"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEGATTSSRV?"), strlen(ESP32("AT+BLEGATTSSRV?")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);


    cout <<"[INFO] 10.ESP32 Bluetooth LE 服务端发现特征"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLEGATTSCHAR?"), strlen(ESP32("AT+BLEGATTSCHAR?")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    while(1)
    {
        usleep(1000000);
        ret = ctrl_uart.uart_read(get_buffer,2048);
        if(ret !=0 )
        {
            cout <<"[INFO] 12."<<get_buffer << endl;
            break;
        }
    }
    cout <<"[INFO] 13.ESP32 Bluetooth LE 服务端配置 Bluetooth LE SPP"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLESPPCFG=1,1,7,1,5"), strlen(ESP32("AT+BLESPPCFG=1,1,7,1,5")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    cout <<"[INFO] 14.ESP32 Bluetooth LE 服务端使能 Bluetooth LE SPP"<< endl;
    ctrl_uart.uart_send(ESP32("AT+BLESPP"), strlen(ESP32("AT+BLESPP")));
    usleep(1000000);
    ret = ctrl_uart.uart_read(get_buffer,2048);
    cout << get_buffer << endl;
    memset(get_buffer,0x00,ret);

    while (1)
    {
        char touchuan[1000] = {0x00};
        cout << "输入任意字符开启传输:" << endl;
        cin >> touchuan;
        for(int i=0;i<1000;i++)
        {
            if(touchuan[i] == 0x00)
            {
                break;
            }
            ctrl_uart.uart_send(touchuan+i,1);
        }
    }

}


